package com.meihua.shellapp.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.meihua.shellapp.R;
import com.meihua.shellapp.adapter.ShareAdapter;
import com.meihua.shellapp.utils.ConstantUtils;
import com.meihua.shellapp.utils.LogUtils;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

/**
 * 作者：lc on 2017/10/28.
 * 邮箱：lc0607live@163.com
 */

public class ShareDialog extends Dialog implements View.OnClickListener, AdapterView.OnItemClickListener {

    private Activity activity;

    private GridView gv_share;//分享
    private TextView tv_cencle;//取消
    private LinearLayout ll_ll;//包裹
    private Integer[] drawables = new Integer[]{R.drawable.umeng_socialize_qq, R.drawable.umeng_socialize_qzone};//图片集合
    private String[] names = new String[]{"QQ", "QQ空间"};//分享app名
    private SHARE_MEDIA[] media = new SHARE_MEDIA[]{SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE};//分享类型

    public ShareDialog(@NonNull Activity context) {
        super(context, R.style.MyDialog);
        this.activity = context;
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.gravity = Gravity.BOTTOM;
        lp.width = ConstantUtils.getInstance().getScreenW(context);
        getWindow().setAttributes(lp);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_share);
        initView();
    }

    private void initView() {
        gv_share = findViewById(R.id.gv_share);
        tv_cencle = findViewById(R.id.tv_cencle);
        ll_ll = findViewById(R.id.ll_ll);
        FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) ll_ll.getLayoutParams();
        lp.width = ConstantUtils.getInstance().getScreenW(getContext());
        ll_ll.setLayoutParams(lp);
        gv_share.setAdapter(new ShareAdapter(activity, names, drawables));
        tv_cencle.setOnClickListener(this);
        gv_share.setOnItemClickListener(this);
    }

    @Override
    public void onClick(View view) {
        dismiss();
    }


    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        UMWeb web = new UMWeb("http://www.baidu.com");
        web.setTitle("baidu");//标题
        UMImage thumb = new UMImage(activity, R.mipmap.ic_launcher);
        web.setThumb(thumb);  //缩略图
        web.setDescription("保险大讲堂");//描述
        new ShareAction(activity)
                .setPlatform(media[i])
                .setCallback(shareListener)
                .withMedia(web)
                .share();
    }

    private UMShareListener shareListener = new UMShareListener() {
        /**
         * @descrption 分享开始的回调
         * @param platform 平台类型
         */
        @Override
        public void onStart(SHARE_MEDIA platform) {
            LogUtils.getInstance().e("开始");
        }

        /**
         * @descrption 分享成功的回调
         * @param platform 平台类型
         */
        @Override
        public void onResult(SHARE_MEDIA platform) {
            LogUtils.getInstance().e("成功");
        }

        /**
         * @descrption 分享失败的回调
         * @param platform 平台类型
         * @param t 错误原因
         */
        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
//            ToastUtils.getInstance().showToast("失败");
            LogUtils.getInstance().e("失败" + t.toString());
        }

        /**
         * @descrption 分享取消的回调
         * @param platform 平台类型
         */
        @Override
        public void onCancel(SHARE_MEDIA platform) {
            LogUtils.getInstance().e("取消");
        }
    };
}
